﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Legend.Component.Bullet
{
    public class BulletRenderable : Interface.IRenderable
    {
        private VertexPositionTexture[] vertices = null;
        private int[] indices = null;
        private Texture arrowTexture = null;
        private Effect effect;
        private VertexDeclaration vertexDecl = null;
        private BoundingBox boundingBox;

        public BoundingBox LoadResource(ContentManager contentManager)
        {
            if (arrowTexture == null)
            {
                arrowTexture = contentManager.Load<Texture>("Arrows");
            }

            if (vertices == null)
            {
                vertices = new VertexPositionTexture[4];
                vertices[0].Position.X = 10f;
                vertices[0].Position.Y = -20f;
                vertices[0].Position.Z = 0f;
                vertices[0].TextureCoordinate.X = 1;
                vertices[0].TextureCoordinate.Y = 1;

                vertices[1].Position.X = -10f;
                vertices[1].Position.Y = -20f;
                vertices[1].Position.Z = 0f;
                vertices[1].TextureCoordinate.X = 0;
                vertices[1].TextureCoordinate.Y = 1;

                vertices[2].Position.X = 10f;
                vertices[2].Position.Y = 20f;
                vertices[2].Position.Z = 0f;
                vertices[2].TextureCoordinate.X = 1;
                vertices[2].TextureCoordinate.Y = 0;

                vertices[3].Position.X = -10f;
                vertices[3].Position.Y = 20f;
                vertices[3].Position.Z = 0f;
                vertices[3].TextureCoordinate.X = 0;
                vertices[3].TextureCoordinate.Y = 0;

                boundingBox.Max = new Vector3(10, 20, 300);
                boundingBox.Min = new Vector3(-10, -20, -300);
            }

            if (indices == null)
            {
                indices = new int[] { 0, 1, 2, 3 };
            }

            effect = contentManager.Load<Effect>("GroundEffect");
            return boundingBox;
        }

        public void SetTextureUV(Vector2 min, Vector2 max)
        {
            vertices[0].TextureCoordinate.X = max.X;
            vertices[0].TextureCoordinate.Y = max.Y;
            vertices[1].TextureCoordinate.X = min.X;
            vertices[1].TextureCoordinate.Y = max.Y;
            vertices[2].TextureCoordinate.X = max.X;
            vertices[2].TextureCoordinate.Y = min.Y;
            vertices[3].TextureCoordinate.X = min.X;
            vertices[3].TextureCoordinate.Y = min.Y;
        }

        public override void Render(GraphicsDevice device)
        {
            device.RenderState.DepthBufferEnable = true;
            device.RenderState.AlphaBlendEnable = true;
            device.RenderState.BlendFunction = BlendFunction.Add;
            device.RenderState.SourceBlend = Blend.SourceAlpha;
            device.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
            if (vertexDecl == null || vertexDecl.GraphicsDevice != device)
            {
                vertexDecl = new VertexDeclaration(device, VertexPositionTexture.VertexElements);
            }
            device.VertexDeclaration = vertexDecl;

            effect.Begin();
            effect.Parameters["Texture"].SetValue(arrowTexture);
            effect.Parameters["World"].SetValue(WorldMatrix);
            effect.Parameters["Projection"].SetValue(ProjectionMatrix);
            effect.Parameters["View"].SetValue(ViewMatrix);

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();
                device.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices, 0, 4, indices, 0, 2);
                pass.End();
            }
            device.RenderState.AlphaBlendEnable = false;
            effect.End();
        }
    }
}
